package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type OrdersLogDAO struct {
	bizID int64
}

type OrdersLogListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newOrdersLogDAO(bizID int64) *OrdersLogDAO {
	return &OrdersLogDAO{
		bizID: bizID,
	}
}

func (c *OrdersLogDAO) MGetOrdersLog(ctx context.Context, ids []int64) (map[int64]*model.OrdersLog, error) {
	list, err := OrdersLog.WithContext(ctx).Where(
		OrdersLog.BizID.Eq(c.bizID),
		OrdersLog.ID.In(ids...),
		OrdersLog.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetOrdersLog err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.OrdersLog)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *OrdersLogDAO) GetOrdersLogByConditions(ctx context.Context, condition []gen.Condition) (*model.OrdersLog, error) {
	tx := OrdersLog.WithContext(ctx).Where(
		OrdersLog.BizID.Eq(c.bizID),
		OrdersLog.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersLogByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *OrdersLogDAO) GetOrdersLogList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.OrdersLog, error) {
	tx := OrdersLog.WithContext(ctx).Where(OrdersLog.BizID.Eq(c.bizID), OrdersLog.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersLogList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *OrdersLogDAO) GetOrdersLogTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := OrdersLog.WithContext(ctx).Where(OrdersLog.BizID.Eq(c.bizID), OrdersLog.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersLogTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *OrdersLogDAO) GetOrdersLogInfoByID(ctx context.Context, id int64) (*model.OrdersLog, error) {
	info, err := OrdersLog.WithContext(ctx).Where(OrdersLog.BizID.Eq(c.bizID), OrdersLog.DeletedAt.IsNull()).Where(OrdersLog.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetOrdersLogInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *OrdersLogDAO) GetListWithTotal(ctx context.Context, params *OrdersLogListParams) ([]*model.OrdersLog, int64, error) {
	tx := OrdersLog.WithContext(ctx).Where(OrdersLog.BizID.Eq(c.bizID), OrdersLog.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.OrdersLog.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *OrdersLogDAO) CreateOrdersLog(ctx context.Context, data *model.OrdersLog) (*model.OrdersLog, error) {
	if err := OrdersLog.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateOrdersLog error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *OrdersLogDAO) CreateOrdersLogWithTx(ctx context.Context, tx *Query, data *model.OrdersLog) (*model.OrdersLog, error) {
	if err := tx.OrdersLog.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateOrdersLog error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *OrdersLogDAO) BatchCreateOrdersLog(ctx context.Context, data []*model.OrdersLog) error {
	if err := OrdersLog.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesOrdersLog error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersLogDAO) BatchCreateOrdersLogWithTx(ctx context.Context, tx *Query, data []*model.OrdersLog) ([]*model.OrdersLog, error) {
	if err := tx.OrdersLog.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxOrdersLog error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *OrdersLogDAO) UpdateOrdersLog(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := OrdersLog.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateOrdersLog error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersLogDAO) UpdateOrdersLogWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.OrdersLog.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateOrdersLog error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersLogDAO) DeleteOrdersLog(ctx context.Context, condition []gen.Condition) error {
	_, err := OrdersLog.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteOrdersLog error: %v", err)
		return err
	}
	return nil
}

func (c *OrdersLogDAO) DeleteOrdersLogWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.OrdersLog.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteOrdersLogWithTx error: %v", err)
		return err
	}
	return nil
}
